/**
 * 受信クラス
 * @author h_kato 
 * @version 0.9.0
 * @see DataRegister.java
 */
package jp.ac.aiit.yc.sww;

import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.ServiceAdapter;

public class SwwMessagingAdapter extends ServiceAdapter {

    @Override
    public Object invoke(Message message) {
    	try{
    		
    		System.out.println("invoke:Start");
    		
    		//メッセージを取得する。
    		AsyncMessage newMessage = (AsyncMessage)message;
    		
    		int rslt = 0;
    		
            
    		//メッセージヘッダ取得    		
    		//操作(operation)
    		String operation = "none";
    		if (message.getHeader("operation") != null)
    		{ 
    			operation = message.getHeader("operation").toString();
    			System.out.println(operation);
    		}    		
    		if(operation.equals("none")){	//ログイン処理
    		
    			System.out.println("Login:" + operation);
    			
    		}else{						//DB登録
    			
    			System.out.println("DB:" + operation);
    			
    			DataRegister dr = new DataRegister();    			
    			rslt = dr.registerData(newMessage, operation);  
    			dr.finalize();
    			
    			MessageService msgService = (MessageService)getDestination().getService();
                msgService.pushMessageToClients(message, true);  
                
                System.out.println("DBend:" + operation);
    		}           
            
    		
    	}catch(Exception e){
    		e.printStackTrace();
    		System.out.println(e.toString());
    	}
        return null;
        
    }
}
